package edu.whu.assignment5.service.impl;

import edu.whu.assignment5.dataobject.Product;
import edu.whu.assignment5.repository.ProductRepository;
import edu.whu.assignment5.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductRepository productRepository;

    @Override
    public Product findProductById(int pid) {
        return productRepository.findByPid(pid);
    }

    @Override
    public List<Product> findAll() {
        return productRepository.findAll();
    }

    @Override
    public Product addProduct(Product product) {
        return productRepository.save(product);
    }

    @Override
    public int deleteProduct(int pid) {
        return productRepository.deleteByPid(pid);
    }

    @Override
    public int updateProduct(Product product) {
        Product same = productRepository.findByPid(product.getPid());
        if(same == null) {
            return -1;  //表示用户不存在
        }
        else {
            Product save = productRepository.save(product);
            return save == null ? 0 : 1;
        }
    }
}
